<template>
  <div class="content">
    <van-nav-bar
      title="支付订单详情"
      left-text="返回"
      left-arrow
      @click-left="Pages()"
    >
    </van-nav-bar>

    <div>
      <van-cell
        @click="Onadrsslist(item)"
        v-for="(item, index) in addressList"
        :key="index"
      >
        <div class="text1">
          <p>{{ item.address }}</p>
        </div>
      </van-cell>
    </div>

    <div
      v-for="(item, index) in goodsList"
      :key="index"
      class="text2"
      style="margin-top: 20px"
    >
      <van-cell-group inset>
        <van-card
          :num="item.count"
          :price="item.price"
          :desc="item.desc"
          :title="item.name"
          :thumb="item.picUrl"
        />
      </van-cell-group>
      <van-submit-bar button-text="确认" @submit="Ondialog" />
    </div>
  </div>
</template>

 <script>
import { Dialog } from "vant";
import { Toast } from "vant";
import axios from "axios";
export default {
  data() {
    return {
      addressList: [],
      radio: 1,
      goodsList: [],
    };
  },
  mounted() {
    this.getData();

    if (this.$route.query.allCheckedGoodsList) {
      this.goodsList = this.$route.query.allCheckedGoodsList;
    } else {
      this.goodsList.push(this.$route.query);
    }
    // console.log(this.goodsList);
  },
  methods: {
    Pages() {
      this.$router.go(-1);
    },
    getData() {
      axios({
        url: "http://127.0.0.1:3000/address/list",
        method: "get",
      }).then((res) => {
        this.addressList = res.data;
      });
    },
    Onadrsslist(item) {
      this.$router.push({
        path: "/addresslist",
        query: {
          id: item.id,
          allCheckedGoodsList: this.goodsList,
        },
      });
    },
    Ondialog() {
      Dialog.confirm({
        title: "支付订单",
        message: "是否确认支付",
      })
        .then(() => {
          Toast.success("支付成功");
          // console.log(this.goodsList);
          axios({
            url: "http://127.0.0.1:3000/index/pay",
            method: "post",
            data: {
              payGoods: this.goodsList,
            },
          });
          // {
          //        ...this.goods,
          //      add:this.goodsList,}
          axios({
            //加入订单
            url: "http://127.0.0.1:3000/index/addOrder",
            method: "post",
            data: this.goodsList,
          });
          this.$router.push({
            path: "/cart",
          });
        })
        .catch(() => {
          Toast.fail("支付失败");
          this.$router.push({
            path: "/cart",
          });
        });
    },
  },
};
</script>

<style>
.content {
  height: 100%;
  background: #fafafa;
}
.text1 {
  height: 20px;
}
.text2 {
  margin-top: 20px;
}
</style>